<?php
class ZTrendCollection
{
    /**
     * @var ZTrendItem[]
     */
    public $TrendItems;

    /**
     * @return ZTrendCollection
     */
    function __construct()
    {
        $this->TrendItems = array();
    }

    /**
     * @return int
     */
    public function count()
    {
        return count($this->TrendItems);
    }

    /**
     * @param $index
     * @return ZTrendItem
     */
    public function getIndex($index)
    {
        return $this->TrendItems[$index];
    }

    /**
     * @param $item $ZTrendItem
     */
    public function addItem($item)
    {
        $this->TrendItems[] = $item;
    }

    /**
     * @param $arr
     * @param $item
     * @param $pos
     * @return array
     */
    private function putItem($arr, $item, $pos){
        return array_merge(array_slice($arr,0,$pos+1), array($item), array_slice($arr,$pos+1));
    }

    /**
     * Filling missing date in range
     */
    public function fillCollection(){
        $size = $this->count();
        $arr = $this->TrendItems;
        /*for ($i = 0; $i < $size - 1; $i++){
            while ((strtotime($arr[$i + 1]->getDate()) - strtotime($arr[$i]->getDate())) > 86400)
            {
                $curDate = strtotime($arr[$i]->getDate()) + 86400;
                $newArr = new ZTrendItem(date('d/m/Y'), 0);
                $arr = $this->putItem($arr, $newArr, $i);
                $i++;
                $size++;
            }
        }*/
        $this->TrendItems = $arr;
    }
    /**
     * @param $days Array Day
     * @param $trends Array Trend of Day
     */
    public function dayTrend(&$days, &$trends){
        foreach ($this->TrendItems as $Item){
            $date = new DateTime($Item->getDate());
            $days[] = $date->format('Y M d');
            $trends[] = array(
                'y'     => $Item->getTrend(),
                'begin' => $date->format('d-m-Y'),
                'end'   => $date->format('d-m-Y')
            );
        }
    }
    /**
     * @param $months Array Month
     * @param $trends Array Trend of month
     */
    public function monthTrend(&$months, &$trends){
        $date = new DateTime($this->TrendItems[0]->getDate());
        $month = $date->format('Y M');
        $trend = 0;
        foreach($this->TrendItems as $Item){
            $itemDate = new DateTime($Item->getDate());
            $itemMonth = $itemDate->format('Y M');
            if ($month==$itemMonth){
                $trend+=$Item->getTrend();
            }
            else{
                $months[] = $month;
                $trends[] = $trend;
                $month = $itemMonth;
                $trend = $Item->getTrend();
            }
        }
        $months[] = $month;
        $trends[] = $trend;
    }

    /**
     * @param $weeks Array Week
     * @param $trends Array Trend
     */
    public function weekTrend(&$weeks, &$trends){
        $date = new DateTime($this->TrendItems[0]->getDate());
        $week = $date->format("W-Y");
        $begin = $date->format("d");
        $end = null;
        $trend = 0;
        foreach ($this->TrendItems as $Item){
            $itemDate = new DateTime($Item->getDate());
            $itemWeek = $itemDate->format("W-Y");
            if ($week == $itemWeek){
                $trend += $Item->getTrend();
                $end = $itemDate->format("M d");
            }
            else{
                if ($begin==$end){
                    $weeks[] = $begin;
                }
                else{
                    $weeks[] = $begin." to ".$end;
                }
                $trends[]   = $trend;
                $week       = $itemWeek;
                $trend      = $Item->getTrend();
                $begin      = $itemDate->format("d");
                $end        = $itemDate->format("M d");
            }
        }
        if ($begin==$end){
            $weeks[] = $begin;
        }
        else{
            $weeks[] = $begin." to ".$end;
        }
        $trends[] = $trend;
    }
}

?>